package com.bookmanagement.service.impl;

import com.bookmanagement.constant.BookException;
import com.bookmanagement.entity.Book;
import com.bookmanagement.entity.Comment;
import com.bookmanagement.entity.User;
import com.bookmanagement.exception.BusinessException;
import com.bookmanagement.mapper.BookMapper;
import com.bookmanagement.mapper.CommentMapper;
import com.bookmanagement.mapper.UserMapper;
import com.bookmanagement.service.CommentService;
import com.bookmanagement.utils.BaseContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CommentServiceImpl implements CommentService {

    @Autowired
    private CommentMapper commentMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private BookMapper bookMapper;

    @Override
    public void comment(Integer id, String content) {
        // 获取当前用户邮箱
        String currentEmail = BaseContext.getCurrentId();
        
        // 查询用户信息
        User user = userMapper.queryByEmail(currentEmail);
        if (user == null) {
            throw new BusinessException("用户不存在");
        }
        
        // 验证图书是否存在
        Book book = bookMapper.getById(Long.valueOf(id));
        if (book == null) {
            throw new BusinessException(BookException.BOOK_NOT_FOUND);
        }
        
        // 创建评论对象
        Comment comment = Comment.builder()
                .bookId(Long.valueOf(id))
                .userId(Long.valueOf(user.getId()))
                .content(content)
                .build();
        
        // 插入评论
        commentMapper.addComment(comment);
    }

    @Override
    public void deleteComment(Long commentId) {
        // 获取当前用户邮箱
        String currentEmail = BaseContext.getCurrentId();
        
        // 查询用户信息
        User user = userMapper.queryByEmail(currentEmail);
        if (user == null) {
            throw new BusinessException("用户不存在");
        }
        
        // 验证评论是否存在
        Comment comment = commentMapper.getCommentById(commentId);
        if (comment == null) {
            throw new BusinessException("评论不存在");
        }
        
        // 验证是否是评论的所有者
        if (!comment.getUserId().equals(Long.valueOf(user.getId()))) {
            throw new BusinessException("无权删除此评论");
        }
        
        // 删除评论
        commentMapper.deleteComment(commentId, Long.valueOf(user.getId()));
    }

    @Override
    public List<Comment> getAllComments() {
        return commentMapper.getAllComments();
    }

    @Override
    public void deleteCommentByAdmin(Long commentId) {
        // 验证评论是否存在
        Comment comment = commentMapper.getCommentById(commentId);
        if (comment == null) {
            throw new BusinessException("评论不存在");
        }
        
        // 管理员删除，不验证用户
        commentMapper.deleteCommentByAdmin(commentId);
    }

    @Override
    public List<Comment> getCommentsByBookId(Long bookId) {
        return commentMapper.getCommentsByBookId(bookId);
    }
}
